<?php

/**
 * @author gbfun <gbfun@163.com>  2018-04-15 20:49
 * http://www.wuweiyou.com
 */

class db_model_good_category
{
    protected $_db_table;   
    protected $_formatter;
    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    private function __clone()
    {
    }
    
    protected function __construct()
    {
    }

    public function get_db_table()
    {
        if (null === $this->_db_table) {
            $this->_db_table = db_table_good_category::get_instance();
        }
        
        return $this->_db_table;
    }
    
    public function get_formatter()
    {
        if (null === $this->_formatter) {
            $this->_formatter = formatter_good_category::get_instance();
        }
    
        return $this->_formatter;
    }
    
    public function set_rows(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row($row);            
        }
    }
    
    public function set_row(& $row)
    {
        $formatter = $this->get_formatter();
        
        $formatter->set_by_create_time($row);
        $formatter->set_by_update_time($row);
        $formatter->set_by_status($row);
    }
    
    public function set_rows_user(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row_user($row);
        }
    }
    
    public function set_row_user(& $row)
    {
        if(!empty($row['create_time '])){
            $this->get_formatter()->set_by_create_time($row);
        }
        $this->get_formatter()->set_by_update_time($row);
    }
    
    public function find($id)
    {
        $row = $this->get_db_table()->find($id);
        //var_dump($row); exit();
    
        if(!empty($row)){
            $this->set_row($row);
            //var_dump($row); exit();
        }
    
        return $row;
    }
    
    public function fetch_all($where = null, $order = null)
    {
        $rows = $this->get_db_table()->fetch_all($where, $order);
        //var_dump($rows); exit();
    
        $this->set_rows($rows);
        //var_dump($rows); exit();
    
        return $rows;
    }
    
    /**
     * 获取【启用】的【直接】子分类
     *     1)若在【后台】，则一般供外部模型使用
     */
    public function get_direct_children($parent_id = 0)
    {   
        $where = array('status' => '1');
        $order = array('`order` asc');
        $where['parent_id'] = intval($parent_id);        
        $direct_children = $this->fetch_all($where, $order);
    
        return $direct_children;
    }
    
    public function get_children($parent_id = 0, $where = null, $order = null)
    {
        $children = array();
						
		$where['parent_id'] = intval($parent_id);				
		$rows = $this->fetch_all($where, $order);
		unset($where['parent_id = ?']);
		foreach($rows as $row){
			$cat             = array('info' => null, 'children' => array());
			$cat['info']     = $row;
		    $cat['children'] = $this->get_children($row['id'], $where, $order);
			
			$children[] = $cat;
		}		

		return $children;
    }
    
    /**
     * 获取所有【启用】的分类【树】
     *     1)供外部模型使用
     */
    public function get_all_category_tree()
    {
        $parent_id = 0;
        $where = array('status' => '1');
        $order = array('`order` asc');
        $all_category_tree = $this->get_children($parent_id, $where, $order);
        //var_dump($all_category_tree); exit();
    
        return $all_category_tree;
    }
    
    public function get_children_list($parent_id = 0, $level = 1, $where = null, $order = null)
    {
        $children_list = array();
                
        $where['parent_id'] = intval($parent_id);
        $rows = $this->fetch_all($where, $order);
        unset($where['parent_id = ?']);
        
        $next_level = $level + 1;
        foreach($rows as $row){
            $row['level']   = $level;
            $children_list[$row['id']] = $row;
            
            $children_list_2 = $this->get_children_list($row['id'], $next_level, $where, $order); 
            
            $children_list = $children_list + $children_list_2;
        }
    
        return $children_list;
    }
    
    
    /**
     * 获取所有【启用】的分类
     *     1)供外部模型使用
     */
    public function get_all_category_list()
    {        
        $parent_id = 0;
        $where = array('status' => '1');
        $order = array('`order` asc');
        $level = 1;
        $all_category_list = $this->get_children_list($parent_id, $level, $where, $order);
        //var_dump($all_category_list); exit();
    
        return $all_category_list;
    }

    
    /**
     * 获取当前分类，及其子分类的【id列表】
     */
    public function get_self_children_id_list($id)
    {
        $self_children_id_list = array($id);
        $where = array();
        $level = 1;
        $children_list = $this->get_children_list($id, $level, $where);
        //var_dump($children_list); exit();
        $children_id_list = array_keys($children_list);
        //var_dump($children_id_list); exit();
        $self_children_id_list = array_merge($self_children_id_list, $children_id_list);
        //var_dump($self_children_id_list); exit();
    
        return $self_children_id_list;
    }
    
    /**
     * 【父分类】不能是【当前分类】及其【子分类】
     */
    public function get_except_parent_id_list($id)
    {
        $except_parent_id_list = $this->get_self_children_id_list($id);
        
        return $except_parent_id_list;
    }
    
    public function get_index_cats($id)
	{
		if($id > 0){
    	    $cat = $this->find($id);
    		//var_dump($cat); exit();
    		
    		$parent_id = $cat['parent_id'];
    		$index_cats = $this->get_index_cats($parent_id);	
    		
    		$index_cats['index_cat_ids'][$id]   = $id;
    		$index_cats['index_cat_names'][$id] = $cat['name'];		
		}else{
		    $index_cats = array();
		    $index_cats['index_cat_ids'][0]   = '0';
		    $index_cats['index_cat_names'][0] = '所有分类';
		}
		
		return $index_cats;
	}
}

